﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

namespace Jy123.Web.PersonalInfo
{
    public partial class CutPortrait : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int userID;
            int photoID = 0;
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);
            string strPhotoID = Request.QueryString["photoID"];
            if (!string.IsNullOrEmpty(strPhotoID))
            {
                photoID = Convert.ToInt32(strPhotoID);
            }
            //else
            //{
            //    photoID = new Jy123.BLL.User.Album().GetPortraitID(userID);                
            //}
            string type = Request.QueryString["Type"];
            if (type == "SaveCutedPhoto")
            {
                string strx1 = Request.Form["x1"];
                string strx2 = Request.Form["x2"];
                string stry1 = Request.Form["y1"];
                string stry2 = Request.Form["y2"];
                string strFilePath = Request.Form["filepath"];
                if(!string.IsNullOrEmpty(strx1)&!string.IsNullOrEmpty(strx2)&!string.IsNullOrEmpty(stry1)&!string.IsNullOrEmpty(stry2))
                {
                    int x1 = Convert.ToInt32(Convert.ToDouble(strx1));
                    int x2 = Convert.ToInt32(Convert.ToDouble(strx2));
                    int y1 = Convert.ToInt32(Convert.ToDouble(stry1));
                    int y2 = Convert.ToInt32(Convert.ToDouble(stry2));
                    //string newPhotoFilePath = new AppCode.CreateDir().CutImage(x2-x1, y2-y1, x1, x2, y1, y2, strFilePath);
                    string newPhotoFilePath = new AppCode.CreateDir().CutImage(100, 100, x1, x2, y1, y2, strFilePath);

                    //同时更换用户主表中的头像为新剪切后的图片地址
                    Jy123.Model.User_Main mUser = new Jy123.Model.User_Main();
                    Jy123.BLL.User_Main bllUser = new Jy123.BLL.User_Main();
                    mUser = bllUser.GetModel(userID);
                    mUser.Portrait = newPhotoFilePath;
                    bllUser.Update(mUser);
                    Response.Redirect("/home/index.aspx");
                }
            }
            //页面初始化
            StringBuilder sbHtml = new StringBuilder(Cache["user_cutimg"].ToString());//最终写出页面的大sb
            sbHtml = sbHtml.Replace("$Header$", Cache["user_header"].ToString());//头部
            sbHtml = sbHtml.Replace("$Footer$", Cache["user_footer"].ToString());//底部
            sbHtml = sbHtml.Replace("$LeftUserCmd$", Cache["user_navigation_up"].ToString());//左侧导航
            sbHtml = sbHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());//左侧直接添加好友
            if (!string.IsNullOrEmpty(strPhotoID))
            {
                sbHtml = sbHtml.Replace("$PhotoFilePath$", new Jy123.BLL.User_Photo().GetModel(photoID).FilePath);//加入需要被切割的图片
            }
            else
            {
                sbHtml = sbHtml.Replace("$PhotoFilePath$", new Jy123.BLL.User_Main().GetModel(userID).Portrait);//加入需要被切割的图片 
            }
            Response.Write(sbHtml);
        }
    }
}
